import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import svgLoader from 'vite-svg-loader'
import EslintPlugin from 'vite-plugin-eslint'
import Components from 'unplugin-vue-components/vite'
import CompressionPlugin from 'vite-plugin-compression'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import visualizer from 'rollup-plugin-visualizer'
import versionUpdatePlugin from './src/utils/versionUpdatePlugin'; //Rollup 的虚拟模块

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, __dirname)
  const CurrentTimeVersion = new Date().getTime();


  return {
    base: '/',
    define: {
      // 定义全局变量
      'process.env.VITE__APP_VERSION__': CurrentTimeVersion,
      '__NEXT_VERSION__': JSON.stringify(process.env.npm_package_version),
    },
    plugins: [
      vue(),
      svgLoader(),
      EslintPlugin({ fix: true }),
      CompressionPlugin({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz'
      }),
      Components({
        resolvers: [AntDesignVueResolver()],
        dts: 'types/components.d.ts',
        dirs: ['src/components']
      }),
      visualizer({
        open: true
      }),
      versionUpdatePlugin({
        edition: CurrentTimeVersion
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            'primary-color': '#ff4d4f',
            'border-radius-base': '3px'
          },
          javascriptEnabled: true
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: 3104,
      open: true,
      https: false,
      proxy: {
        '/api': {
          target: env.VITE_PROXY_URL,
          changeOrigin: true
        }
      }
    }
  }
})
